/*
 *  Copyright (c) 2009, Hugo Augusto Alves
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *        notice, this list of conditions and the following disclaimer in the
 *        documentation and/or other materials provided with the distribution.
 *      * Neither the name of the Hugo Augusto Alves nor the
 *        names of its contributors may be used to endorse or promote products
 *        derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
 *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.liteml.parser;

import org.liteml.parser.blocks.LiteBlockFactory;
import org.liteml.parser.blocks.LiteBlock;
import java.util.ArrayList;
import java.util.Date;
import org.liteml.exceptions.LiteParseException;

import org.liteml.parser.LiteUtils.Target;

/**
 *
 * @author Hugo
 */
public class LiteParser {

    private String input;
    private StringBuilder output;
    private ArrayList<LiteBlock> blocks;
    private Target target;

    public LiteParser() {
    }

    public LiteParser(String str) {
        setInput(str);
    }

    public String parse(Target target) {
        if(input == null || input.equals("")) return "";
        this.target = target;
        processOutput();
        return output.toString();
    }

    private void processInput() {
        String[] x = input.split("\n\n");
        for (String s : x) {
            LiteBlock lb = null;
            try {
                lb = LiteBlockFactory.createBlock(s);
                if (lb != null) {
                    blocks.add(lb);
                }
            } catch (LiteParseException ex) {
                System.err.println("Error on analyzing the block:\n" + s + "\nMessage: " + ex.getMessage());
            }

        }

    }

    private void processOutput() {
        for (LiteBlock block : blocks) {
            output.append(block.parse(target));
            output.append("\n");
        }
    }

    public void setInput(String input) {
        this.input = input;
        this.output = new StringBuilder();
        this.blocks = new ArrayList<LiteBlock>();
        processInput();
    }



    public static void main(String[] args) {
//        ArrayList<Integer> list = LiteMLToken.indexesOf("x*****y", LiteMLToken.token.bold);
//        for (Integer i : list) {
//            System.err.println(i);
//        }
//        System.err.println(LiteMLText.bold);
//        System.err.println(LiteMLText.italic);
//        System.err.println(LiteMLText.strike);


        //String str = "|>>>= Name                   |>>= Age   |";
//        String str = "|: label                              |";
//        for(String s : str.split(LiteUtils.TABLE_SEPARATOR)) {
//            System.err.println("[" + s + "]");
//        }

        /** /
        String test = "= Título1 =\n\n"
                + "== Título2 ==\n\n"
                + "+ Título1 +\n\n"
                + "++ Título2 ++\n\n"
                + "== Título2\n\n"
                + "-----\n\n"
                + "++ Título3 ++\n\n"
                + "* Lista1\n"
                + "** lista2\n"
                + "** lista3\n"
                + "* lista4\n\n"
                + "* Lista1\n"
                + "** lista2\n"
                + "** lista3\n\n"
                + "=== Título3 ===\n\n"
                + "* \n\n"
                + "------\n\n"
                + "{{image.png}}\n\n"
                + "{{name|image.png}}\n\n"
                + "|= First Name |= Last Name |= Age |\n"
                + "| Hugo        | Alves      | 22   |\n"
                + "| xXxXxX      | xxXXxx     | xX   |\n"
                + "|: label                          |\n\n"
                + "|>>>= Name                  |>>= Age  |\n"
                + "|^ Hugo     | A | Alves     | 22 | 11 |\n"
                + "|^          | X | xxXXxx    | xX | Xx |\n"
                + "|: label                              |\n\n"
                + "{{{\n%%pre%%formatted\n}}}\n\n"
                + "- it is %%not%% a comment statement with **bold //italic bold//**, //italic//, __underline__, --strike--, ^^sup^^, ~~sub~~, [[links]], [[links|links.html]] [[and more links]] and even\\\\- break lines!!! (c) (r) (tm) >> <<" +
                "http://www.test.com  \n\n"
                + "\n\n";

        /**/
        String test = "== página 01 ==";
        Date before = new Date();
        String res = new LiteParser(test).parse(Target.HTML);
        System.out.println("Parsed in " + (new Date().getTime() - before.getTime()) + "ms.\n" + res);
        /**/
    }
}
